﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FPSO.HMI.Controls
{
    /// <summary>
    /// UCTimeView.xaml 的交互逻辑
    /// </summary>
    public partial class UCTimeView : UserControl
    {
        public Action<int, int> TimeClick;

        public UCTimeView(int currentHour,int currentMinute)
        {
            InitializeComponent();
            
            Hour = currentHour;
            Minute = currentMinute;
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            ControlUpdate();
        }

        private void BtnConfirm_Click(object sender, RoutedEventArgs e)
        {
            TimeClick?.Invoke(Hour, Minute);
        }


        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            TimeClick?.Invoke(-1, -1);
        }

        private void HourBtn_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            Hour = Convert.ToInt32(btn.Content.ToString());
            ControlUpdate();
        }

        private void MinuteBtn_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            Minute = Convert.ToInt32(btn.Content.ToString());
            ControlUpdate();
        }

        public int Hour { get; set; }
        public int Minute { get; set; }

        private void ControlUpdate()
        {
            Style HourStyle = (Style)FindResource("HourStyle");
            Style MinuteStyle = (Style)FindResource("MinuteStyle");
            Style SelectHourStyle = (Style)FindResource("SelectHourStyle");
            Style SelectMinuteStyle = (Style)FindResource("SelectMinuteStyle");

            foreach(UIElement ui in panelHour.Children)
            {
                if (ui is Button btn)
                {
                    if (Convert.ToInt32(btn.Content.ToString()) == Hour)
                    {
                        btn.Style = SelectHourStyle;
                    }
                    else
                    {
                        btn.Style = HourStyle;
                    }
                }
            }

            foreach(UIElement ui in panelMinute.Children)
            {
                if (ui is Button btn)
                {
                    if (Convert.ToInt32(btn.Content.ToString()) == Minute)
                    {
                        btn.Style = SelectMinuteStyle;
                    }
                    else
                    {
                        btn.Style = MinuteStyle;
                    }
                }
            }

            txbTime.Text = string.Format("{0}:{1}:00", Hour, Minute);
        }
    }
}
